
local U = require "packages/hanqing/util"

local quefan = fk.CreateSkill {
  name = "hanqing__quefan&",
  tags = { U.HorizontalAlliance },
}

Fk:loadTranslationTable{
  ["hanqing__quefan&"] = "雀返",
  [":hanqing__quefan&"] = "<a href='hanqing__alliance_skill'>连横技</a>，"..
  "你每回合使用的第一张基本牌或普通锦囊牌具有助战：目标+1，若为同势力角色助战再+1。",
}

quefan:addEffect(fk.AfterCardUseDeclared, {
  anim_type = "control",
  can_trigger = function(self, event, target, player, data)
    if target == player and player:hasSkill(quefan.name) and
      U.getAlliance(player) == "horizontal" and
      (data.card.type == Card.TypeBasic or data.card:isCommonTrick()) then
      local use_events = player.room.logic:getEventsOfScope(GameEvent.UseCard, 1, function (e)
        local use = e.data
        return use.from == player and (use.card.type == Card.TypeBasic or use.card:isCommonTrick())
      end, Player.HistoryTurn)
      if #use_events > 0 and use_events[1].data == data then
        return #data:getExtraTargets() > 0
      end
    end
  end,
  on_cost = Util.TrueFunc,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local n = 0
    if data.extra_data and table.contains(data.extra_data.variation_type or {}, "@zhuzhan") then
      n = 1
    else
      local targets = table.filter(room:getOtherPlayers(player, false), function (p)
        return ((p:hasSkill(quefan.name) and p.kingdom == "qin") or not p:isKongcheng()) and not table.contains(data.tos, p)
      end)
      if #targets > 0 then
        local extra_data = {
          num = 1,
          min_num = 1,
          include_equip = false,
          skillName = "#variation&",
          pattern = ".|.|.|.|.|"..data.card:getTypeString(),
        }
        local dat = {
          "discard_skill",
          "#hanqing__quefan-zhuzhan:"..player.id.."::"..data.card:getTypeString()..":"..data.card:toLogString(),
          true,
          extra_data,
        }
        local req = Request:new(targets, "AskForUseActiveSkill")
        req.focus_text = "@zhuzhan"
        req.n = 1
        for _, p in ipairs(targets) do
          if p:hasSkill(quefan.name) then
            dat[4].min_num = 0
          else
            dat[4].min_num = 1
          end
          req:setData(p, dat)
        end
        req:ask()
        local winner = req.winners[1]
        if winner then
          local result = req:getResult(winner)
          local ids = result
          if result ~= "" then
            if result.card then
              ids = result.card.subcards
            else
              ids = result
            end
          end
          if winner.kingdom == player.kingdom then
            n = 2
          else
            n = 1
          end
          if #ids > 0 then
            room:throwCard(ids, "#variation&", winner, winner)
          end
        end
      end
    end
    if n > 0 and not player.dead and #data:getExtraTargets() > 0 then
      local tos = room:askToChoosePlayers(player, {
        min_num = n,
        max_num = n,
        targets = data:getExtraTargets(),
        skill_name = quefan.name,
        prompt = "#hanqing__quefan-choose:::"..data.card:toLogString()..":"..n,
        cancelable = true,
      })
      if #tos > 0 then
        room:sortByAction(tos)
        for _, p in ipairs(tos) do
          data:addTarget(p)
        end
      end
    end
  end,
})

return quefan
